package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ICompanyEmpNecessaryDAO;
import org.jsola.hr.dto.CompanyEmpNecessaryAddDTO;
import org.jsola.hr.dto.CompanyEmpNecessaryUpdateDTO;
import org.jsola.hr.entity.CompanyEmpNecessaryDO;
import org.jsola.hr.query.CompanyEmpNecessaryQuery;
import org.jsola.hr.service.ICompanyEmpNecessaryService;
import org.jsola.hr.vo.CompanyEmpNecessaryListVO;
import org.jsola.hr.vo.CompanyEmpNecessaryVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公司员工必填模块信息
 *
 * @author zhr
 */
@Service("hrCompanyEmpNecessaryServiceImpl")
public class CompanyEmpNecessaryServiceImpl implements ICompanyEmpNecessaryService {

    @Autowired
    private ICompanyEmpNecessaryDAO companyEmpNecessaryDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public CompanyEmpNecessaryVO save(CompanyEmpNecessaryAddDTO companyEmpNecessaryAddDTO, TokenUser tokenUser) {
        // 转DO
        CompanyEmpNecessaryDO companyEmpNecessaryDO = companyEmpNecessaryAddDTO.to(CompanyEmpNecessaryDO.class);
        // 保存
        companyEmpNecessaryDO = save(companyEmpNecessaryDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return companyEmpNecessaryDO.to(CompanyEmpNecessaryVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(CompanyEmpNecessaryUpdateDTO companyEmpNecessaryUpdateDTO, TokenUser tokenUser) {
        //转DO
        CompanyEmpNecessaryDO companyEmpNecessaryDO = companyEmpNecessaryUpdateDTO.to(CompanyEmpNecessaryDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(companyEmpNecessaryDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... companyEmpNecessaryIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) companyEmpNecessaryIds);
    }


    @Override
    public CompanyEmpNecessaryVO selectById(String companyEmpNecessaryId, String siteId) {
        CompanyEmpNecessaryDO companyEmpNecessaryDO = selectDOById(companyEmpNecessaryId, siteId);
        if (companyEmpNecessaryDO == null) {
            return null;
        }
        return companyEmpNecessaryDO.to(CompanyEmpNecessaryVO.class);
    }

    @Override
    public List<CompanyEmpNecessaryListVO> select(CompanyEmpNecessaryQuery companyEmpNecessaryQuery, String siteId) {
        List<CompanyEmpNecessaryDO> companyEmpNecessaryDOList = selectDO(companyEmpNecessaryQuery, siteId);
        if (CollectionUtils.isEmpty(companyEmpNecessaryDOList)) {
            return companyEmpNecessaryDOList == null ? null : new ArrayList<>();
        }
        return companyEmpNecessaryDOList.stream()
                .map(companyEmpNecessaryDO -> companyEmpNecessaryDO.to(CompanyEmpNecessaryListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<CompanyEmpNecessaryVO> selectByCompanyId(String companyId, TokenUser tokenUser) {
        Example example = new Example(CompanyEmpNecessaryDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", tokenUser.getSiteId())
                .andEqualTo("companyId", companyId);
        List<CompanyEmpNecessaryDO> companyEmpNecessaryDOList = companyEmpNecessaryDAO.selectByExample(example);
        if (CollectionUtils.isEmpty(companyEmpNecessaryDOList) || companyEmpNecessaryDOList.size() < 18) {
            List<String> moduleCodeList = companyEmpNecessaryDOList.parallelStream()
                    .map(CompanyEmpNecessaryDO::getModuleCode).collect(Collectors.toList());
            insertToCompany(companyId, moduleCodeList, tokenUser);
            return selectByCompanyId(companyId, tokenUser);
        } else {
            return companyEmpNecessaryDOList.stream()
                    .map(item -> item.to(CompanyEmpNecessaryVO.class)).collect(Collectors.toList());
        }

    }

    @Override
    public int selectCount(CompanyEmpNecessaryQuery companyEmpNecessaryQuery, String siteId) {
        Example example = buildExample(companyEmpNecessaryQuery, siteId);
        return companyEmpNecessaryDAO.selectCountByExample(example);
    }

    @Override
    public Page<CompanyEmpNecessaryListVO> selectPage(CompanyEmpNecessaryQuery companyEmpNecessaryQuery, String siteId) {
        Example example = buildExample(companyEmpNecessaryQuery, siteId);
        Page<CompanyEmpNecessaryDO> page = companyEmpNecessaryDAO.selectPageByExample(example,
                companyEmpNecessaryQuery.getPageNo(),
                companyEmpNecessaryQuery.getPageSize());

        return page.to(CompanyEmpNecessaryListVO.class);
    }

    @Override
    public CompanyEmpNecessaryDO selectDOById(String companyEmpNecessaryId, String siteId) {
        return listById(companyEmpNecessaryId, siteId);
    }

    @Override
    public List<CompanyEmpNecessaryDO> selectDO(CompanyEmpNecessaryQuery companyEmpNecessaryQuery, String siteId) {
        Example example = buildExample(companyEmpNecessaryQuery, siteId);
        return companyEmpNecessaryDAO.selectByExample(example);
    }

    /**
     * 查询时补充小程序中个人信息必填项 下发insertCompany为创建用工单位初始化方法
     *
     * @param companyId   用工单位Id
     * @param moduleCodes 用工单位已创建的个人信息必填项
     * @param tokenUser   登录用户
     */
    private void insertToCompany(String companyId, List<String> moduleCodes, TokenUser tokenUser) {
        List<CompanyEmpNecessaryDO> list = new ArrayList<>();
        List<String> moduleNameList = new ArrayList<>();
        List<String> moduleCodeList = new ArrayList<>();
        if (!moduleCodes.contains("basic")) {
            moduleNameList.add("基本信息");
            moduleCodeList.add("basic");
        }
        if (!moduleCodes.contains("contact")) {
            moduleNameList.add("联系信息");
            moduleCodeList.add("contact");
        }
        if (!moduleCodes.contains("empFamily")) {
            moduleNameList.add("家庭成员");
            moduleCodeList.add("empFamily");
        }
        if (!moduleCodes.contains("empContacts")) {
            moduleNameList.add("紧急联系人");
            moduleCodeList.add("empContacts");
        }
        if (!moduleCodes.contains("empEducation")) {
            moduleNameList.add("教育经历");
            moduleCodeList.add("empEducation");
        }
        if (!moduleCodes.contains("empWork")) {
            moduleNameList.add("工作经历");
            moduleCodeList.add("empWork");
        }
        if (!moduleCodes.contains("empTrain")) {
            moduleNameList.add("培训经历");
            moduleCodeList.add("empTrain");
        }
        if (!moduleCodes.contains("empIdCard")) {
            moduleNameList.add("身份证信息");
            moduleCodeList.add("empIdCard");
        }

        if (!moduleCodes.contains("empState")) {
            moduleNameList.add("员工状态");
            moduleCodeList.add("empState");
        }
        if (!moduleCodes.contains("onJob")) {
            moduleNameList.add("在职信息");
            moduleCodeList.add("onJob");
        }
        if (!moduleCodes.contains("file")) {
            moduleNameList.add("档案信息");
            moduleCodeList.add("file");
        }
        if (!moduleCodes.contains("rewardsPunishments")) {
            moduleNameList.add("奖惩记录");
            moduleCodeList.add("rewardsPunishments");
        }
        if (!moduleCodes.contains("retire")) {
            moduleNameList.add("退休信息");
            moduleCodeList.add("retire");
        }

        if (!moduleCodes.contains("bank")) {
            moduleNameList.add("工资卡信息");
            moduleCodeList.add("bank");
        }
        if (!moduleCodes.contains("socAndPf")) {
            moduleNameList.add("社保公积金信息");
            moduleCodeList.add("socAndPf");
        }

        if (!moduleCodes.contains("annex")) {
            moduleNameList.add("附件资料");
            moduleCodeList.add("annex");
        }
        if (!moduleCodes.contains("archive")) {
            moduleNameList.add("员工档案资料");
            moduleCodeList.add("archive");
        }
        if (!moduleCodes.contains("leaveOffice")) {
            moduleNameList.add("员工离职资料");
            moduleCodeList.add("leaveOffice");
        }
        for (int a = 0; a < moduleCodeList.size(); a++) {
            CompanyEmpNecessaryDO companyEmpNecessaryDO = new CompanyEmpNecessaryDO();
            companyEmpNecessaryDO.setModuleName(moduleNameList.get(a));
            companyEmpNecessaryDO.setModuleCode(moduleCodeList.get(a));
            companyEmpNecessaryDO.setIsNecessary(false);
            companyEmpNecessaryDO.setCompanyId(companyId);
            companyEmpNecessaryDO.preInsert(tokenUser.getUserId());
            companyEmpNecessaryDO.setSiteId(tokenUser.getSiteId());
            list.add(companyEmpNecessaryDO);
        }
        companyEmpNecessaryDAO.insertListAndSetId(list);
    }

    @Override
    public void insertCompany(String companyId, TokenUser tokenUser) {
        List<CompanyEmpNecessaryDO> list = new ArrayList<>();
        List<String> moduleNameList = new ArrayList<>();
        List<String> moduleCodeList = new ArrayList<>();
        moduleNameList.add("基本信息");
        moduleNameList.add("联系信息");
        moduleNameList.add("家庭成员");
        moduleNameList.add("紧急联系人");
        moduleNameList.add("教育经历");
        moduleNameList.add("工作经历");
        moduleNameList.add("培训经历");
        moduleNameList.add("身份证信息");

        moduleNameList.add("员工状态");
        moduleNameList.add("在职信息");
        moduleNameList.add("档案信息");
        moduleNameList.add("奖惩记录");
        moduleNameList.add("退休信息");

        moduleNameList.add("工资卡信息");
        moduleNameList.add("社保公积金信息");

        moduleNameList.add("附件资料");
        moduleNameList.add("员工档案资料");
        moduleNameList.add("员工离职资料");


        moduleCodeList.add("basic");
        moduleCodeList.add("contact");
        moduleCodeList.add("empFamily");
        moduleCodeList.add("empContacts");
        moduleCodeList.add("empEducation");
        moduleCodeList.add("empWork");
        moduleCodeList.add("empTrain");
        moduleCodeList.add("empIdCard");

        moduleCodeList.add("empState");
        moduleCodeList.add("onJob");
        moduleCodeList.add("file");
        moduleCodeList.add("rewardsPunishments");
        moduleCodeList.add("retire");

        moduleCodeList.add("bank");
        moduleCodeList.add("socAndPf");

        moduleCodeList.add("annex");
        moduleCodeList.add("archive");
        moduleCodeList.add("leaveOffice");

        for (int a = 0; a < moduleCodeList.size(); a++) {
            CompanyEmpNecessaryDO companyEmpNecessaryDO = new CompanyEmpNecessaryDO();
            companyEmpNecessaryDO.setModuleName(moduleNameList.get(a));
            companyEmpNecessaryDO.setModuleCode(moduleCodeList.get(a));
            if (a < 1) {
                companyEmpNecessaryDO.setIsNecessary(true);
            } else {
                companyEmpNecessaryDO.setIsNecessary(false);
            }
            companyEmpNecessaryDO.setCompanyId(companyId);

            companyEmpNecessaryDO.preInsert(tokenUser.getUserId());
            companyEmpNecessaryDO.setSiteId(tokenUser.getSiteId());

            list.add(companyEmpNecessaryDO);
        }
        companyEmpNecessaryDAO.insertListAndSetId(list);
    }

    @Override
    public List<CompanyEmpNecessaryDO> listBySystem() {
        Example example = new Example(CompanyEmpNecessaryDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("companyId", HrConstants.DEFAULT)
                .andEqualTo("siteId", HrConstants.DEFAULT);
        example.orderBy("moduleCode");
        return companyEmpNecessaryDAO.selectByExample(example);
    }

    @Override
    public List<CompanyEmpNecessaryDO> listAll() {
        Example example = new Example(CompanyEmpNecessaryDO.class);
        example.and()
                .andEqualTo("valid", true);
        return companyEmpNecessaryDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example
     *
     * @param companyEmpNecessaryQuery 查询参数
     * @param siteId                   所属站点id
     * @return example
     */
    private Example buildExample(CompanyEmpNecessaryQuery companyEmpNecessaryQuery, String siteId) {
        Example example = new Example(CompanyEmpNecessaryDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (companyEmpNecessaryQuery.getModuleName() != null) {
            example.and().andEqualTo("moduleName", companyEmpNecessaryQuery.getModuleName());
        }
        if (companyEmpNecessaryQuery.getModuleCode() != null) {
            example.and().andEqualTo("moduleCode", companyEmpNecessaryQuery.getModuleCode());
        }
        if (companyEmpNecessaryQuery.getIsNecessary() != null) {
            example.and().andEqualTo("isNecessary", companyEmpNecessaryQuery.getIsNecessary());
        }
        if (companyEmpNecessaryQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", companyEmpNecessaryQuery.getCompanyId());
        }
        // 排序
        ExampleKit.setExampleOrder(example, companyEmpNecessaryQuery.getOrders());
        return example;
    }
}




